package com.syxd.controller;

import com.syxd.common.annotation.LogOperation;
import com.syxd.common.constant.Constant;
import com.syxd.common.entity.WebResult;
import com.syxd.common.utils.PageData;
import com.syxd.dto.SysSunCreditDto;
import com.syxd.service.SysSunAuditService;
import com.syxd.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Api(tags = "阳光信贷的审批管理")
@RequestMapping("sys/sunAudit")
public class SysSunAuditController {
    @Autowired
    SysSunAuditService sysSunAuditService;
    @Autowired
    SysUserService sysUserService;
    @PostMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "branchName", value = "支行名", paramType = "query", dataType = "String")
    })
    @LogOperation(apiType = "syssunAudit", apiPath = "sys/sunAudit")
    @RequiresPermissions("public:sysjiang:page")
    public WebResult<PageData<SysSunCreditDto>> page(@ApiIgnore @RequestBody Map<String, Object> params,HttpServletRequest request) {

        String branchName = (String) sysUserService.getInfoFromToken(request, "branchName").getData();

        //   String branchName = (String) sysUserService.getInfoFromToken(request, "branchName").getData();
        if (branchName!=null){
            params.put("branchName",branchName);
        }
        PageData<SysSunCreditDto> page = sysSunAuditService.page(params);
        return new WebResult<PageData<SysSunCreditDto>>().ok(page);
    }
    @GetMapping("{id}")
    @LogOperation(apiType = "syssunAudit", apiPath = "sys/sunAudit")
    @RequiresPermissions("public:sysjiang:page")
    public WebResult   query( @PathVariable("id") Long id ){


        return new WebResult().ok(sysSunAuditService.selectById(id));

    }


    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @LogOperation(apiType = "syssunAudit", apiPath = "sys/sunAudit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "中间数据id", paramType = "query", required = true, dataType = "int")

    })
    @RequiresPermissions("approval:sysjiang:del:")
    public WebResult delete(@PathVariable("id") Long id, HttpServletRequest request) {
        return sysSunAuditService.deleteSunCreditById(id, request);

    }


    @PostMapping("{id}")
    @ApiOperation("新增")
    @LogOperation(apiType = "syssunAudit", apiPath = "sys/sunAudit")
    @RequiresPermissions("approval:sysjiang:add:")
    public WebResult add(@PathVariable("id") Long id, HttpServletRequest request) {

        return sysSunAuditService.add(id, request);

    }

    @PutMapping("{id}")
    @ApiOperation("修改")
    @LogOperation(apiType = "syssunAudit", apiPath = "sys/sunAudit")
    @RequiresPermissions("approval:sysjiang:update:")
    public WebResult put(@PathVariable("id") Integer id, HttpServletRequest request) {
        return sysSunAuditService.put(id, request);


    }

    /**
     * 拒绝请求
     */
    @GetMapping("/refuse")
    @ApiOperation("拒绝修改")
    @LogOperation(apiType = "syssunAudit", apiPath = "sys/sunAudit")
    @RequiresPermissions("approval:")
    public WebResult refuse(@RequestParam Integer id, HttpServletRequest request){

       return   sysSunAuditService.refuse(id,request);

    }
}
